package com.ocom.payhandle.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ocom.common.entity.card.EntityXfRecordProcess;
import com.ocom.common.vo.pyhandle.LimitProcessVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface IDaoXfRecordProcess extends BaseMapper<EntityXfRecordProcess> {


    @Select({"<script>",
            "select p_id,sum(times) times,sum(amount) amount  from (\n" +
                    "select   p_id,IFNULL(plat_order_no,order_no) order_no, case when  max(if_refund)>0 then 0 else 1 end  times\n" +
                    ", case when  max(if_refund)>0 then 0 else 0-real_money end  amount \n" +
                    "from  xf_record_process \n" +
                    "where  com_id=#{comId} \n" +
                    "  and p_id =#{pId}   \n" +
                    " <when test=\"beginDate!=endDate\"> " +
                        "<![CDATA[" +
                        " and  meal_date between  #{beginDate}  and   #{endDate}  \n" +
                        "]]>" +
                    " </when> " +
                    " <when test=\"beginDate == endDate\"> " +
                        "<![CDATA[" +
                        " and  meal_date = #{endDate}  \n" +
                        "]]>" +
                    " </when> " +
                    " <when test=\"mealNo!=null and mealNo>=0\"> " +
                    " AND  meal_no=#{mealNo}  " +
                    " </when> " +
                    " and cons_state =1 and  (pay_state =1  or  (orig_order_no is not null  and plat_order_no is not null  ))\n" +
                    "group  by p_id,IFNULL(plat_order_no,order_no)\n" +
                    " )T  group  by p_id" +
                    "</script>"})
    LimitProcessVo getWaitPayLimit(
            @Param("comId") Long comId,
            @Param("pId") Long pId,
            @Param("mealNo") Integer mealNo,
            @Param("beginDate") String beginDate,
            @Param("endDate") String endDate
    );



    @Select({"<script>",
            "   insert into ocom_saas_xfrecord_0${part}.xf_record_${month} ( " +
                    " com_id,device_no,device_name,point_id,point_name,merch_id,merch_name,receipt_id,receipt_name,receipt_no," +
                    " dept_id,dept_name,p_id,p_code,p_name,account_no,card_no,charge_balance,sub_balance,cons_time,meal_date,meal_no," +
                    " discount,cons_money,real_money,charge_money,sub_money,cons_state,cons_type,op_type,op_type_diff,order_no,plat_order_no," +
                    " pay_channel_no,channel_order_no,pay_way,orig_order_no,if_refund,pay_time,pay_state,pay_amount,refund_time,discount_way," +
                    " channel_discount,channel_fee,acc_type_id,sub_account,admin_id,admin_name,voucher_type,voucher_no,voucher_number,deduction," +
                    " par_value,verification,face,reserved1,reserved2,reserved3,send_state,month_value,create_time,cancel_state,receipt_price" +
                    " )   SELECT  pro.com_id,pro.device_no,pro.device_name,pro.point_id,pro.point_name,pro.merch_id,pro.merch_name,pro.receipt_id,pro.receipt_name,pro.receipt_no, +\n" +
                    "                    pro.dept_id,pro.dept_name,pro.p_id,pro.p_code,pro.p_name,pro.account_no,pro.card_no,pro.charge_balance,pro.sub_balance,pro.cons_time,pro.meal_date,pro.meal_no, \n" +
                    "                     pro.discount,pro.cons_money,pro.real_money,pro.charge_money,pro.sub_money,pro.cons_state,pro.cons_type,pro.op_type,pro.op_type_diff,pro.order_no,pro.plat_order_no,  \n" +
                    "                    pro.pay_channel_no,pro.channel_order_no,pro.pay_way,pro.orig_order_no,pro.if_refund,pro.pay_time,pro.pay_state,pro.pay_amount,pro.refund_time,pro.discount_way,  \n" +
                    "                    pro.channel_discount,pro.channel_fee,pro.acc_type_id,pro.sub_account,pro.admin_id,pro.admin_name,pro.voucher_type,pro.voucher_no,pro.voucher_number,pro.deduction,  \n" +
                    "                    pro.par_value,pro.verification,pro.face,pro.reserved1,pro.reserved2,pro.reserved3,pro.send_state,pro.month_value,pro.create_time,pro.cancel_state,pro.receipt_price    FROM  \n" +
                    "  ocom_saas_device_${part}.v_xf_record_process  pro  " +
                    "  LEFT JOIN ocom_saas_xfrecord_0${part}.xf_record_${month}  recode ON pro.order_no = recode.order_no  " +
                    "   where " +
                    "   pro.month_value = #{month}  " +
                    "   AND pro.pay_state = 1 " +
                    "   AND recode.order_no is NULL \n" +
//                    "   AND pro.cons_state <> 4  \n" +
//                    "   AND pro.create_time > '2025-04-01 00:00:00' \n" +
//                    "   and pro.cons_money <> 0 \n" +
//                    "    LIMIT 50  " +
            "</script>"})
    List<EntityXfRecordProcess> timerTransportOrder(@Param("month")String month,@Param("part")Integer part);


    @Select({"<script>",
            "   INSERT INTO ocom_saas_xfrecord_0${part}.xf_goods_list_${month} ( com_id, device_no, point_id, merch_id, p_id, dept_id, receipt_id, receipt_name, order_no, cons_time, meal_date, meal_no, cons_type, if_refund, dishes_id, dishes_name, price_mode, weight_number, dishes_price, unit_price, month_value, create_time)  \n" +
                    "select p.com_id,p.device_no,p.point_id,p.merch_id,p.p_id,p.dept_id,p.receipt_id,p.receipt_name,p.order_no,p.cons_time,p.meal_date,p.meal_no,p.cons_type,p.if_refund,p.dishes_id,p.dishes_name,p.price_mode,p.weight_number,p.dishes_price,p.unit_price,p.month_value,p.create_time  \n" +
                    "from ocom_saas_xfrecord_0${part}.xf_record_${month} x  \n" +
                    "left join ocom_saas_device_${part}.xf_goods_list_process p on p.com_id=x.com_id and p.order_no =x.order_no \n" +
                    "left join  ocom_saas_xfrecord_0${part}.xf_goods_list_${month} g on x.com_id=g.com_id and x.order_no =g.order_no \n" +
                    "where   x.cons_state in (3,5) and x.if_refund=0 and x.pay_state=1  and p.p_id is not null and  g.id is null  \n" +
                    "</script>"})
    List<EntityXfRecordProcess> timerTransportGoodList(@Param("month")String month,@Param("part")Integer part);
}
